package welsir.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import welsir.entity.dos.Article;
import welsir.entity.dos.ArticleComment;
import welsir.entity.vo.ArticleCommentVO;
import welsir.entity.vo.ResultMessage;
import welsir.mapper.ArticleCommentMapper;
import welsir.mapper.ArticleMapper;
import welsir.service.IArticleCommentService;
import welsir.util.ResultUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Genius
 * @date 2023/03/08 01:29
 **/
@Api(value = "web端文章评论Controller")
@RestController
@RequestMapping("/webArticleComment")
public class WebArticleCommentController {
    @Resource
    private ArticleCommentMapper articleCommentMapper;

    @Autowired
    private IArticleCommentService articleCommentService;

    @Resource
    private ArticleMapper articleMapper;

    /**
     * 根据文章id获取评论
     * @param articleId
     * @return
     */
    @ApiOperation(value = "根据文章uid获取评论")
    @GetMapping("/getAllArticleComment/{articleId}")
    public ResultMessage<List<ArticleComment>> getAllArticleComment(
            @PathVariable("articleId") String articleId
    ) {

        QueryWrapper<ArticleComment> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id",articleId);

        return ResultUtil.data(articleCommentMapper.selectList(wrapper));
    }

    /**
     * 增加评论
     * @param articleComment
     * @return
     */
    @ApiOperation(value = "新增评论")
    @PostMapping("/addArticleComment")
    public ResultMessage<Integer> addArticleComment(@RequestBody ArticleComment articleComment) {
        articleComment.setAddTime(new Date());
        return ResultUtil.data(articleCommentMapper.insert(articleComment));
    }

    /**
     * 查看文章对应的评论数
     * @param articleId
     */
    @ApiOperation(value = "查看文章对应的评论数")
    @GetMapping("/getArticleCommentNum/{articleId}")
    public ResultMessage<Integer> getArticleCommentNum(
            @PathVariable("articleId") Integer articleId
    ) {

        return ResultUtil.data(articleCommentService.GetCommentNum(articleId));
    }

    /**
     * 获得最新评论
     *  @param num
     */
    @ApiOperation(value = "获取最新评论")
    @GetMapping("/getNewArticleComment/{num}")
    public ResultMessage<List<ArticleCommentVO>> getNewArticleComment(
            @PathVariable("num") Integer num
    ) {
        List<ArticleCommentVO> result = new ArrayList<>();
        QueryWrapper<ArticleComment> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("add_time");
        //使用last方法拼接sql语句
        wrapper.last("limit "+ num);
        List<ArticleComment> articleComments = articleCommentMapper.selectList(wrapper);
        for (ArticleComment articleComment : articleComments) {
            ArticleCommentVO articleCommentVO = new ArticleCommentVO();
            String articleId = articleComment.getArticleId();
            //根据文章id查询文章名称
            Article article = articleMapper.selectOne(new QueryWrapper<Article>().eq("article_id", articleId));
            String title = article.getTitle();
            articleCommentVO.setArticleName(title);
            BeanUtils.copyProperties(articleComment,articleCommentVO);

            result.add(articleCommentVO);
        }
        return ResultUtil.data(result);
    }

}
